package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.ExternalBlackList;
import com.ruoyi.project.system.domain.SiyuBlacklist;
import com.ruoyi.project.system.mapper.SiyuBlacklistMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/black")
public class SiyuBlackListController extends BaseController {

    @Autowired
    private SiyuBlacklistMapper siyuBlacklistMapper;

    @Anonymous
    @GetMapping("/inter")
    public List<Map<String, Object>> interQuery(SiyuBlacklist blacklist) {
        if (StringUtils.isEmpty(blacklist.getName()) || StringUtils.isEmpty(blacklist.getCardNo())){
            return new ArrayList<>();
        }
        return siyuBlacklistMapper.queryBlackList(blacklist);
    }

    @GetMapping("/labels")
    public AjaxResult labels() {
        return success(siyuBlacklistMapper.labels('0'));
    }

    @PostMapping("/setLabels")
    @Transactional
    public AjaxResult setLabels(@RequestBody List<String> labels) {
        List<String> oldLabels = siyuBlacklistMapper.labels(null);
        // 新增加
        List<String> newAdds = labels.stream().filter(e -> !oldLabels.contains(e)).collect(Collectors.toList());
        for (String label : newAdds) {
            siyuBlacklistMapper.addLabels(label, SecurityUtils.getUsername());
        }
        // 已移除
        List<String> newRemoves = oldLabels.stream().filter(e -> !labels.contains(e)).collect(Collectors.toList());
        for (String label : newRemoves) {
            siyuBlacklistMapper.setLabels(label, '1', SecurityUtils.getUsername());
        }
        // 交集
        Collection<String> collection = CollectionUtils.intersection(labels, oldLabels);
        for (String label : collection) {
            siyuBlacklistMapper.setLabels(label, '0', SecurityUtils.getUsername());
        }
        return success();
    }

    /**
     * 请求查询
     */
    @Log(title = "商户查询黑名单列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:black:comquery')")// MGZ TODO 2025/3/10:
    @GetMapping("/blackList")
    public List<SiyuBlacklist> selectBlackList(SiyuBlacklist blacklist) {
        Boolean isEmpty = StringUtils.isEmpty(blacklist.getName()) && StringUtils.isEmpty(blacklist.getCardNo()) && StringUtils.isEmpty(blacklist.getTel());
        if (isEmpty) {
            return null;
        }
        return siyuBlacklistMapper.queryBlackListByOrThreeElem(blacklist);
    }

    @Log(title = "商户核对黑名单列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:black:comquery')")// MGZ TODO 2025/3/10:
    @GetMapping("/listByElem")
    public AjaxResult listByElem(SiyuBlacklist blacklist) {
        Boolean isEmpty = StringUtils.isEmpty(blacklist.getName()) && StringUtils.isEmpty(blacklist.getCardNo()) && StringUtils.isEmpty(blacklist.getTel());
        if (isEmpty) {
            return error("缺少参数");
        }
        return success(siyuBlacklistMapper.queryBlackListByAndThreeElem(blacklist));
    }

    @PreAuthorize("@ss.hasPermi('system:black:list')")
    @Log(title = "管理员获取黑名单列表", businessType = BusinessType.EXPORT)
    @GetMapping("/list")
    public TableDataInfo list(SiyuBlacklist blacklist) {
        startPage();
        List<SiyuBlacklist> list = siyuBlacklistMapper.selectSiyuBlacklist(blacklist);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:black:query')")
    @Log(title = "管理员获取黑名单", businessType = BusinessType.EXPORT)
    @GetMapping("/{id}")
    public AjaxResult getBlack(@PathVariable("id") Integer id) {
        return success(siyuBlacklistMapper.selectSiyuBlacklistById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:black:add')")// MGZ TODO 2025/3/10:
    @PostMapping()
    public AjaxResult add(@RequestBody SiyuBlacklist blacklist) {
        Boolean isEmpty = StringUtils.isEmpty(blacklist.getName()) && StringUtils.isEmpty(blacklist.getCardNo()) && StringUtils.isEmpty(blacklist.getTel());
        if (isEmpty) {
            return error("缺少参数");
        }
        SiyuBlacklist siyuBlacklist = siyuBlacklistMapper.selectBlackList(blacklist);
        if (siyuBlacklist == null) {
            blacklist.setCreateBy(SecurityUtils.getUsername());
            blacklist.setCreateBy(SecurityUtils.getUsername());
            return success(siyuBlacklistMapper.insertSiyuBlacklist(blacklist));
        } else {
            siyuBlacklist.setLabels(blacklist.getLabels());
            siyuBlacklist.setLocation(blacklist.getLocation());
            siyuBlacklist.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(siyuBlacklistMapper.updateSiyuBlacklist(siyuBlacklist));
        }
    }

    @PreAuthorize("@ss.hasPermi('system:black:edit')")
    @Log(title = "管理员编辑黑名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiyuBlacklist siyuBlacklist) {
        return toAjax(siyuBlacklistMapper.updateSiyuBlacklist(siyuBlacklist));
    }

    /**
     * 删除黑名单
     */
    @PreAuthorize("@ss.hasPermi('system:black:delete')")
    @Log(title = "管理员移除黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(siyuBlacklistMapper.deleteSiyuBlacklistByIds(ids));
    }

    // 云起
    @Anonymous
    @PostMapping("/push")
    public void externalAdd(@RequestBody ExternalBlackList ebl) {
        Boolean isEmpty = StringUtils.isEmpty(ebl.getName()) && StringUtils.isEmpty(ebl.getCardNo()) && StringUtils.isEmpty(ebl.getTel());
        if (!isEmpty) {
            siyuBlacklistMapper.insertSiyuBlacklist(new SiyuBlacklist(ebl));
        }
    }

}
